﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using DataAnnotationsExtensions;
using OGS.Contracts;
using OGS.DataAccess;
using OGS.Utility;

namespace OGS.Web.Models.Admin
{
    public class SaveCategoryModel
    {
        /// <summary>
        /// The ID
        /// </summary>
        public int ID
        {
            get;
            set;
        }

        /// <summary>
        /// The Code
        /// </summary>
        [Display(Name = "Code")]
        [Required(ErrorMessage = "Please enter code.")]
        public string Code
        {
            get;
            set;
        }

        /// <summary>
        /// The Name
        /// </summary>
        [Display(Name = "Name (English)")]
        [Required(ErrorMessage = "Please enter name (english).")]
        public string EnglishName
        {
            get;
            set;
        }

        /// <summary>
        /// The Name
        /// </summary>
        [Display(Name = "Name (Bengali)")]
        [Required(ErrorMessage = "Please enter name (bengali).")]
        public string BengaliName
        {
            get;
            set;
        }

        /// <summary>
        /// The Name
        /// </summary>
        [Display(Name = "Name (Hindi)")]
        [Required(ErrorMessage = "Please enter name (hindi).")]
        public string HindiName
        {
            get;
            set;
        }

        /// <summary>
        /// The Url
        /// </summary>
        [Display(Name = "Url")]
        [Required(ErrorMessage = "Please enter url.")]
        public string Url
        {
            get;
            set;
        }

        /// <summary>
        /// The Image
        /// </summary>
        public string Image { get; set; }

        /// <summary>
        /// Product Image File
        /// </summary>
        [Display(Name = "Image")]
        [ValidateImageFileAttribute(ErrorMessage = "Please upload valid image file")]
        public HttpPostedFileBase CategoryImage { get; set; }
        
        /// <summary>
        /// The Display Order
        /// </summary>
        [Display(Name = "Display Order")]
        [Required(ErrorMessage = "Please enter valid display order.")]
        [Integer(ErrorMessage="Please enter valid display order.")]
        public int DisplayOrder
        {
            get;
            set;
        }

        /// <summary>
        /// The Parent Category ID
        /// </summary>
        [Display(Name = "Parent Category")]
        public int ParentCategoryID
        {
            get;
            set;
        }

        /// <summary>
        /// The Parent Category List
        /// </summary>
        public List<Category> ParentCategories { get; set; }

        /// <summary>
        /// The Root Level
        /// </summary>
        public int RootLevel { get; set; }

        /// <summary>
        /// Checks whether the Category Is Active
        /// </summary>
        [Display(Name = "Is Active")]
        public bool IsActive
        {
            get;
            set;
        }

        public bool IsSuccess { get; set; }

        /// <summary>
        /// Get Category Details
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public void GetCategoryDetails(int ID)
        {
            Category oCategory = DACategory.Instance.GetCategoryDetails(ID);
            this.ID = oCategory.ID;
            this.Code = oCategory.Code;

            if (oCategory.CategoryLanguages.Count >= 3)
            {
                this.EnglishName = oCategory.CategoryLanguages[0].Name;
                this.BengaliName = oCategory.CategoryLanguages[1].Name;
                this.HindiName = oCategory.CategoryLanguages[2].Name;
            }
            this.Url = oCategory.Url;
            this.Image = oCategory.Image;
            this.DisplayOrder = oCategory.DisplayOrder;
            this.ParentCategoryID = oCategory.ParentCategoryID;
            this.IsActive = oCategory.IsActive;
            
        }

        /// <summary>
        /// Populate Parent Categories
        /// </summary>
        public void PopulateParentCategories()
        {
            this.ParentCategories = DACategory.Instance.GetCategories(0);
        }

        /// <summary>
        /// Save Category
        /// </summary>
        public int SaveCategory()
        {
            Category oCategory = new Category();
            oCategory.ID = this.ID;

            //Set Languages
            oCategory.CategoryLanguages.Add(new CategoryLanguage { LanguageID = (int)Language.English, Name = this.EnglishName.Trim() });
            oCategory.CategoryLanguages.Add(new CategoryLanguage { LanguageID = (int)Language.Bengali, Name = this.BengaliName.Trim() });
            oCategory.CategoryLanguages.Add(new CategoryLanguage { LanguageID = (int)Language.Hindi, Name = this.HindiName.Trim() });

            oCategory.Code = this.Code.Trim();
            oCategory.Url = this.Url.Trim();
            oCategory.Image = this.Image != null ? this.Image.Trim() : string.Empty;
            oCategory.DisplayOrder = this.DisplayOrder;
            oCategory.ParentCategoryID = this.ParentCategoryID;
            oCategory.RootLevel = this.ParentCategoryID == 0 ? (short)0 : (short)1;
            oCategory.IsActive = this.IsActive;
            oCategory.LastModifiedBy = Helper.GetCurrentUserID();
            oCategory.LastModifiedDate = DateTime.Now;

            int result = DACategory.Instance.SaveCategory(oCategory);
            this.ID = oCategory.ID;
            return result;
        }
    }
}